'use strict';

/**
 * @name 委派操作，如果flag是0，那么委派的是需求，如果是1，那么委派的是技术, flag是2的话，委派的是进程，那么ID不是URL参数，需要额外传进来, flag是3的话，委派的是用户管理的推荐人
 * @author ZYF
 */

angular.module('metalabOperationApp').controller('AssignModelCtrl', ['$rootScope', '$stateParams', '$state', 'params', '$scope', '$uibModalInstance', 'MetaResources', 'MetaRemind', function($rootScope, $stateParams, $state, params, $scope, $uibModalInstance, MetaResources, MetaRemind) {
    $scope.sure = function() {
        if (params.data.name && (params.flag===3?$rootScope.referenceCandidateId:$rootScope.leaderCandidateId)) {
            var module = ['demand', 'technology', 'demand', 'user'];
            var passData = {};
            if (module[params.flag]==='user') {
              passData = {
                  'referenceId': $rootScope.referenceCandidateId
              };
            } else {
              passData = {
                  'projectManagerId': $rootScope.managerIdOptions[$rootScope.managerOptions.indexOf(params.data.name)],
                  'leaderId': $rootScope.leaderCandidateId
              };
            }
            MetaResources[module[params.flag]].save({
                    'id': (params.flag===2?params.data.id:$stateParams.id)
                }, passData,
                function() {
                    MetaRemind.success("恭喜", "修改成功");
                    $uibModalInstance.dismiss('cancel');
                    $state.reload();
                },
                function(error) {
                    MetaRemind.serverError(error);
                    $uibModalInstance.dismiss('cancel');
                });
        } else {
            if (params.flag===3) {
              MetaRemind.errorInfo("失败", "请先选择好推荐人");
            } else {
              MetaRemind.errorInfo("失败", "请先选择好项目经理和负责人");
            }
            $uibModalInstance.dismiss('cancel');
        }
    };
    $scope.cancel = function() {
        $uibModalInstance.dismiss('cancel');
    };
}]);
